package com.yifi.system.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.model.MenuModel;
import com.yifi.common.utils.ListUtils;
public class MenuService {

	/**
	 * 根据用户获取最基础菜单信息
	 * @param cuser
	 * @return
	 */
	public List<MenuModel> getMenu(String cuser)
	{
		List<MenuModel> menuWork=new ArrayList<MenuModel>();
		
		String sql_w = "select w.ccode as id,w.cname as menuName,w.cdesc,w.cprg as url"
				+ ",w.ctarger as target,wc.icon,w.cclass2 as pid,5 as ilayer,w.isort  from work w,work_class2 wc"
				+ " where w.cclass2=wc.ccode and w.st_flag='1' and wc.st_flag='1' and w.bc_flag like '%bs%'";
		if (!cuser.equals("admin")) // admin
		{
			sql_w += " and (w.ccode in (select cwork from mis_post_menu where cpost in"
					+ " (select cpost from mis_user_post where ccode='"+ cuser + "') or cpost='all') " 
					+ " or w.ccode in (select cwork from mis_user_menu where cuser='"+ cuser + "')" + ")";
		}
		sql_w += " order by wc.ilayer,wc.isort,w.isort";
		List<Record> listW = Db.find(sql_w);
		for (Record record : listW) {
			MenuModel menuModel=new MenuModel();
			menuModel.setId(record.getStr("id"));
			menuModel.setMenuName(record.getStr("menuName"));
			menuModel.setUrl(record.getStr("url"));
			menuModel.setTarget(record.getStr("target"));
			menuModel.setIcon(record.getStr("icon"));
			menuModel.setPid(record.getStr("pid"));
			menuModel.setIsort(record.getInt("isort"));
			
			menuWork.add(menuModel);
		}
		
		return menuWork;
	}
	/**
	 * 根据下级菜单获取上级菜单信息
	 * @param childAll
	 * @return
	 */
	public List<MenuModel> getParent(List<MenuModel> childAll)
	{
		
		List<MenuModel> locMenu=new ArrayList<MenuModel>();
		locMenu.addAll(childAll);
		List<MenuModel> menuAll=new ArrayList<MenuModel>();
		List<String> listStr=new ArrayList<String>();
		String pid="";
		String id="";
		String mid="";
		List<String> listPid=new ArrayList<String>();
		
		for (MenuModel menuChild : childAll) {
			mid=menuChild.getPid();
			if(!listPid.contains(mid)&&!mid.equals("0"))
			{
				listPid.add(menuChild.getPid());
				String sql="select ccode as id,cname as menuName,pclass as pid,'' as url,'' as target,icon,ilayer,isort,st_flag "
						+ " from work_class2 where ccode='"+mid+"'  order by isort";
				
				Record record=Db.findFirst(sql);
				if(record!=null)
				{
					id=record.getStr("id");
					if(!listStr.contains(id))
					{
						List<MenuModel> locList=ListUtils.findModelFromListByPid(locMenu, mid);
						locMenu.removeAll(locList);
						
						//查询是否已经找到当前父级菜单，找到的话只更新子集信息即可
						List<MenuModel> extMenu=ListUtils.findModelFromListById(childAll, id);
						MenuModel menuModel=new MenuModel();
						if(extMenu.size()>0)
						{
							menuModel=extMenu.get(0);
							List<MenuModel> clist = menuModel.getChildren();
							if(clist!=null)
							{
								clist.addAll(locList);
							}
							else
							{
								clist=locList;
							}
							menuModel.setChildren(clist);
						}
						else
						{
							menuModel.setId(id);
							menuModel.setMenuName(record.getStr("menuName"));
							menuModel.setUrl(record.getStr("url"));
							menuModel.setTarget(record.getStr("target"));
							menuModel.setIcon(record.getStr("icon"));
							menuModel.setIsort(record.getInt("isort"));
							pid=record.getStr("pid");
							menuModel.setPid(pid);
							if(record.getStr("st_flag").equals("1"))
							{
								menuModel.setChildren(locList);
								menuAll.add(menuModel);
							}
						}
					}
					listStr.add(id);
				}
			}
		}
		if(locMenu.size()>0)
		{
			menuAll.addAll(locMenu);
		}
		
		if(listStr.size()<=0)
		{
			// 重新排序
			Collections.sort(menuAll, new Comparator<MenuModel>() {
				public int compare(MenuModel arg0, MenuModel arg1) {
					return arg0.getIsort().compareTo(arg1.getIsort());
				}
			});
			return menuAll;
		}
		else
		{
			List<MenuModel> pmenu=getParent(menuAll);
			// 重新排序
			Collections.sort(pmenu, new Comparator<MenuModel>() {
				public int compare(MenuModel arg0, MenuModel arg1) {
					return arg0.getIsort().compareTo(arg1.getIsort());
				}
			});
			return pmenu;
		}
	}
	
	/**
	 * 根据子集菜单获取最顶级菜单
	 * @param childAll
	 * @return
	 */
	public List<MenuModel> getParentMenu(List<MenuModel> childAll)
	{
		List<MenuModel> locMenu=new ArrayList<MenuModel>();
		locMenu.addAll(childAll);
		List<MenuModel> menuAll=new ArrayList<MenuModel>();
		List<String> listStr=new ArrayList<String>();
		String pid="";
		String id="";
		String mid="";
		List<String> listPid=new ArrayList<String>();

		for (MenuModel menuChild : childAll) {
			mid=menuChild.getPid();
			if(!listPid.contains(mid)&&!mid.equals("0"))
			{
				listPid.add(menuChild.getPid());
				String sql="select ccode as id,cname as menuName,pclass as pid,'' as url,'' as target,icon,ilayer,isort,st_flag "
						+ " from work_class2 where ccode='"+mid+"' order by isort";
				
				Record record=Db.findFirst(sql);
				if(record!=null)
				{
					id=record.getStr("id");
					if(!listStr.contains(id))
					{
						List<MenuModel> locList=ListUtils.findModelFromListByPid(locMenu, mid);
						locMenu.removeAll(locList);
						
						//查询是否已经找到当前父级菜单，找到的话只更新子集信息即可
						List<MenuModel> extMenu=ListUtils.findModelFromListById(childAll, id);
						MenuModel menuModel=new MenuModel();
						if(extMenu.size()>0)
						{
							menuModel=extMenu.get(0);
						}
						else
						{
							menuModel.setId(id);
							menuModel.setMenuName(record.getStr("menuName"));
							menuModel.setUrl(record.getStr("url"));
							menuModel.setTarget(record.getStr("target"));
							menuModel.setIcon(record.getStr("icon"));
							menuModel.setIsort(record.getInt("isort"));
							pid=record.getStr("pid");
							menuModel.setPid(pid);
							if(record.getStr("st_flag").equals("1"))
							{
								menuAll.add(menuModel);
							}
						}
					}
					listStr.add(id);
				}
			}
		}
		if(locMenu.size()>0)
		{
			menuAll.addAll(locMenu);
		}
		
		if(listStr.size()<=0)
		{
			// 重新排序
			Collections.sort(menuAll, new Comparator<MenuModel>() {
				public int compare(MenuModel arg0, MenuModel arg1) {
					return arg0.getIsort().compareTo(arg1.getIsort());
				}
			});
			return menuAll;
		}
		else
		{
			List<MenuModel> pmenu=getParentMenu(menuAll);
			// 重新排序
			Collections.sort(pmenu, new Comparator<MenuModel>() {
				public int compare(MenuModel arg0, MenuModel arg1) {
					return arg0.getIsort().compareTo(arg1.getIsort());
				}
			});
			
			return pmenu;
		}
	}
	
	/**
	 * 根据顶级菜单获取子集菜单信息
	 * @param pid
	 * @param cuser
	 */
	public List<MenuModel> getMenuByTopParent(String pid,String cuser)
	{
		List<MenuModel> childAll=getParent(getMenu(cuser));
		List<MenuModel> extMenu=ListUtils.findModelFromListById(childAll, pid);
		if(extMenu.size()>0)
		{
			return extMenu.get(0).getChildren();
		}
		else
		{
			return null;
		}
	}
	
	
	public List<MenuModel> getMenuTop(String cuser)
	{
		List<MenuModel> menuWork=new ArrayList<MenuModel>();
		
		String sql_w = "select w.ccode as id,w.cname as menuName,w.cdesc,w.cprg as url"
				+ ",w.ctarger as target,wc.icon,w.cclass2 as pid,5 as ilayer,w.isort  from work w,work_class2 wc"
				+ " where w.cclass2=wc.ccode and w.st_flag='1' and wc.st_flag='1' and w.bc_flag like '%bs%'";
		if (!cuser.equals("admin")) // admin
		{
			sql_w += " and (w.ccode in (select cwork from mis_post_menu where cpost in"
					+ " (select cpost from mis_user_post where ccode='"+ cuser + "' and cpost='bigscreen') ) )";
		}
		else
		{
			sql_w += " and (w.ccode in (select cwork from mis_post_menu where cpost='bigscreen') ) ";
		}
		
		sql_w += " order by wc.ilayer,wc.isort,w.isort";
		List<Record> listW = Db.find(sql_w);
		for (Record record : listW) {
			MenuModel menuModel=new MenuModel();
			menuModel.setId(record.getStr("id"));
			menuModel.setMenuName(record.getStr("menuName"));
			menuModel.setUrl(record.getStr("url"));
			menuModel.setTarget(record.getStr("target"));
			menuModel.setIcon(record.getStr("icon"));
			menuModel.setPid(record.getStr("pid"));
			menuModel.setIsort(record.getInt("isort"));
			
			menuWork.add(menuModel);
		}
		
		return menuWork;
	}
	
	

	public List<Record> GetMenuTree() {
		String sql = "select ccode as id,cname as name,pclass as pid from work_class2 where st_flag='1' and pclass='0' order by isort";
		List<Record> listTree = Db.find(sql);
		for (Record r : listTree) {
			r.set("open", true);
			r.set("children", GetMenuChild(r.getStr("id")));
		}
		return listTree;
	}

	private List<Record> GetMenuChild(String pid) {
		String sql = "select ccode as id,cname as name,pclass as pid from work_class2 where st_flag='1'  and pclass='"
				+ pid + "'  order by isort";
		List<Record> listChild = Db.find(sql);
		for (Record r : listChild) {
			r.set("children", GetMenuChild(r.getStr("id")));
		}
		return listChild;
	}

	
	//根据权限组获取菜单信息
	public List<MenuModel> getMenuPost(String post)
	{
		List<MenuModel> menuWork=new ArrayList<MenuModel>();
		
		
		String sql_w = "select w.ccode as id,w.cname as menuName,w.cdesc,w.cprg as url"
				+ ",w.ctarger as target,wc.icon,w.cclass2 as pid,5 as ilayer,w.isort"
				+ ",(select count(1) from mis_post_menu where cpost='"+post+"' and cwork=w.ccode) as cknum "
				+ "  from work w,work_class2 wc"
				+ " where w.cclass2=wc.ccode and w.st_flag='1' and wc.st_flag='1' and w.bc_flag like '%bs%'";
		//if(!post.equals(""))
		//{
			//sql_w += " and w.ccode in (select cwork from mis_post_menu where cpost ='"+post+"')" ;
		//}
		sql_w += " order by wc.ilayer,wc.isort,w.isort";
		List<Record> listW = Db.find(sql_w);
		for (Record record : listW) {
			MenuModel menuModel=new MenuModel();
			menuModel.setId(record.getStr("id"));
			menuModel.setMenuName(record.getStr("menuName"));
			//menuModel.setUrl(record.getStr("url"));
			//menuModel.setTarget(record.getStr("target"));
			//menuModel.setIcon(record.getStr("icon"));
			menuModel.setPid(record.getStr("pid"));
			menuModel.setIsort(record.getInt("isort"));

			menuModel.setChecked(record.getInt("cknum")>0?true:false);
			/*
			String sql="select count(1) from mis_post_menu where cpost ='"+post+"' and cwork='"+record.getStr("id")+"'";
			if(Db.queryInt(sql)>0)
			{
				menuModel.setChecked(true);
			}
			*/
			menuWork.add(menuModel);
		}
		
		return menuWork;
	}
}
